//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ciicos.alarm.controller;

import ciicos.alarm.entity.AlarmConfigEntity;
import ciicos.alarm.model.alarmconfig.AlarmConfigExcelErrorVO;
import ciicos.alarm.model.alarmconfig.AlarmConfigExcelVO;
import ciicos.alarm.model.alarmconfig.AlarmConfigForm;
import ciicos.alarm.model.alarmconfig.AlarmConfigInfoVO;
import ciicos.alarm.model.alarmconfig.AlarmConfigListVO;
import ciicos.alarm.model.alarmconfig.AlarmConfigPagination;
import ciicos.alarm.service.AlarmConfigService;
import ciicos.annotation.JnpfField;
import ciicos.base.ActionResult;
import ciicos.base.UserInfo;
import ciicos.base.vo.DownloadVO;
import ciicos.base.vo.PageListVO;
import ciicos.base.vo.PaginationVO;
import ciicos.config.ConfigValueUtil;
import ciicos.exception.DataException;
import ciicos.iview.service.IviewService;
import ciicos.onlinedev.model.OnlineImport.ExcelImFieldModel;
import ciicos.onlinedev.model.OnlineImport.ExcelImportModel;
import ciicos.onlinedev.model.OnlineImport.VisualImportModel;
import ciicos.permission.entity.UserEntity;
import ciicos.point.entity.PointUpdateRecordEntity;
import ciicos.point.result.ScadaEntity;
import ciicos.point.result.ScadaResult;
import ciicos.point.service.PointUpdateRecordService;
import ciicos.util.DateUtil;
import ciicos.util.ExcelUtil;
import ciicos.util.FileUploadUtils;
import ciicos.util.GeneraterSwapUtil;
import ciicos.util.JsonUtil;
import ciicos.util.RandomUtil;
import ciicos.util.StringUtil;
import ciicos.util.UpUtil;
import ciicos.util.UploaderUtil;
import ciicos.util.UserProvider;
import ciicos.util.XSSEscape;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.xuyanwu.spring.file.storage.FileInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import javax.validation.Valid;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Api(
    tags = {"报警配置"},
    value = "alarmManage"
)
@RequestMapping({"/api/alarmManage/AlarmConfig"})
public class AlarmConfigController {
    private static final Logger log = LoggerFactory.getLogger(AlarmConfigController.class);
    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;
    @Autowired
    private ConfigValueUtil configValueUtil;
    @Autowired
    private UserProvider userProvider;
    @Autowired
    private AlarmConfigService alarmConfigService;
    @Autowired
    private IviewService iviewService;
    @Autowired
    private PointUpdateRecordService pointUpdateRecordService;

    @ApiOperation("获取列表")
    @PostMapping({"/getList"})
    public ActionResult list(@RequestBody AlarmConfigPagination alarmConfigPagination) throws IOException {
        List<AlarmConfigEntity> list = this.alarmConfigService.getList(alarmConfigPagination);
        List<AlarmConfigListVO> listVO = JsonUtil.getJsonToList(list, AlarmConfigListVO.class);

        for(AlarmConfigListVO alarmConfigVO : listVO) {
            ActionResult info = this.info(alarmConfigVO.getId());
            Map<String, Object> dataMap = JsonUtil.entityToMap(info.getData());
            alarmConfigVO.setTagname_name(alarmConfigVO.getTagname());
            alarmConfigVO.setTagdesc_name(alarmConfigVO.getTagdesc());
            alarmConfigVO.setTagkind_name(this.generaterSwapUtil.getDicName(alarmConfigVO.getTagkind(), "436925156615521477"));
            alarmConfigVO.setTageu_name(alarmConfigVO.getTageu());
            alarmConfigVO.setEnabledmark_name(this.generaterSwapUtil.switchSelectValue(alarmConfigVO.getEnabledmark(), "启用", "禁用"));
            alarmConfigVO.setTagalarmenable_name(this.generaterSwapUtil.switchSelectValue(alarmConfigVO.getTagalarmenable(), "是", "否"));
            alarmConfigVO.setTagdeadband_name(alarmConfigVO.getTagdeadband());
            alarmConfigVO.setTagalarmdelay_name(alarmConfigVO.getTagalarmdelay());
            alarmConfigVO.setUpdatetime_name(alarmConfigVO.getUpdatetime());
            alarmConfigVO.setTagllpr_name(this.generaterSwapUtil.getDicName(alarmConfigVO.getTagllpr(), "546667323512074629"));
            alarmConfigVO.setTagll_name(alarmConfigVO.getTagll());
            alarmConfigVO.setTaglopr_name(this.generaterSwapUtil.getDicName(alarmConfigVO.getTaglopr(), "546667323512074629"));
            alarmConfigVO.setTaglo_name(alarmConfigVO.getTaglo());
            alarmConfigVO.setTaghipr_name(this.generaterSwapUtil.getDicName(alarmConfigVO.getTaghipr(), "546667323512074629"));
            alarmConfigVO.setTaghi_name(alarmConfigVO.getTaghi());
            alarmConfigVO.setTaghhpr_name(this.generaterSwapUtil.getDicName(alarmConfigVO.getTaghhpr(), "546667323512074629"));
            alarmConfigVO.setTaghh_name(alarmConfigVO.getTaghh());
            alarmConfigVO.setTagdevpr_name(this.generaterSwapUtil.getDicName(alarmConfigVO.getTagdevpr(), "546667323512074629"));
            alarmConfigVO.setTagdev_name(alarmConfigVO.getTagdev());
            alarmConfigVO.setTagsp_name(alarmConfigVO.getTagsp());
            alarmConfigVO.setTagratepr_name(this.generaterSwapUtil.getDicName(alarmConfigVO.getTagratepr(), "546667323512074629"));
            alarmConfigVO.setTagrate_name(alarmConfigVO.getTagrate());
            alarmConfigVO.setTagratecyc_name(alarmConfigVO.getTagratecyc());
            alarmConfigVO.setAlarmpr_name(this.generaterSwapUtil.getDicName(alarmConfigVO.getAlarmpr(), "546667323512074629"));
            alarmConfigVO.setNoalarmval_name(alarmConfigVO.getNoalarmval());
        }

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = (PaginationVO)JsonUtil.getJsonToBean(alarmConfigPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }

    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody @Valid AlarmConfigForm alarmConfigForm) throws DataException {
        String b = this.alarmConfigService.checkForm(alarmConfigForm, 0);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        } else {
            String mainId = RandomUtil.uuId();
            UserInfo userInfo = this.userProvider.get();
            UserEntity userEntity = this.generaterSwapUtil.getUser(userInfo.getUserId());
            AlarmConfigEntity entity = (AlarmConfigEntity)JsonUtil.getJsonToBean(alarmConfigForm, AlarmConfigEntity.class);
            entity.setId(mainId);
            this.alarmConfigService.save(entity);
            return ActionResult.success("创建成功");
        }
    }

    @ApiOperation("导出Excel")
    @PostMapping({"/Actions/Export"})
    public ActionResult Export(@RequestBody AlarmConfigPagination alarmConfigPagination) throws IOException {
        if (StringUtil.isEmpty(alarmConfigPagination.getSelectKey())) {
            return ActionResult.fail("请选择导出字段");
        } else {
            List<AlarmConfigEntity> list = this.alarmConfigService.getTypeList(alarmConfigPagination, alarmConfigPagination.getDataType());
            List<AlarmConfigListVO> listVO = JsonUtil.getJsonToList(list, AlarmConfigListVO.class);

            for(AlarmConfigListVO alarmConfigVO : listVO) {
                ActionResult info = this.info(alarmConfigVO.getId());
                Map<String, Object> dataMap = JsonUtil.entityToMap(info.getData());
                alarmConfigVO.setTagname_name(alarmConfigVO.getTagname());
                alarmConfigVO.setTagdesc_name(alarmConfigVO.getTagdesc());
                alarmConfigVO.setTagkind_name(this.generaterSwapUtil.getDicName(alarmConfigVO.getTagkind(), "436925156615521477"));
                alarmConfigVO.setTageu_name(alarmConfigVO.getTageu());
                alarmConfigVO.setEnabledmark_name(this.generaterSwapUtil.switchSelectValue(alarmConfigVO.getEnabledmark(), "启用", "禁用"));
                alarmConfigVO.setTagalarmenable_name(this.generaterSwapUtil.switchSelectValue(alarmConfigVO.getTagalarmenable(), "是", "否"));
                alarmConfigVO.setTagdeadband_name(alarmConfigVO.getTagdeadband());
                alarmConfigVO.setTagalarmdelay_name(alarmConfigVO.getTagalarmdelay());
                alarmConfigVO.setUpdatetime_name(alarmConfigVO.getUpdatetime());
                alarmConfigVO.setTagllpr_name(this.generaterSwapUtil.getDicName(alarmConfigVO.getTagllpr(), "546667323512074629"));
                alarmConfigVO.setTagll_name(alarmConfigVO.getTagll());
                alarmConfigVO.setTaglopr_name(this.generaterSwapUtil.getDicName(alarmConfigVO.getTaglopr(), "546667323512074629"));
                alarmConfigVO.setTaglo_name(alarmConfigVO.getTaglo());
                alarmConfigVO.setTaghipr_name(this.generaterSwapUtil.getDicName(alarmConfigVO.getTaghipr(), "546667323512074629"));
                alarmConfigVO.setTaghi_name(alarmConfigVO.getTaghi());
                alarmConfigVO.setTaghhpr_name(this.generaterSwapUtil.getDicName(alarmConfigVO.getTaghhpr(), "546667323512074629"));
                alarmConfigVO.setTaghh_name(alarmConfigVO.getTaghh());
                alarmConfigVO.setTagdevpr_name(this.generaterSwapUtil.getDicName(alarmConfigVO.getTagdevpr(), "546667323512074629"));
                alarmConfigVO.setTagdev_name(alarmConfigVO.getTagdev());
                alarmConfigVO.setTagsp_name(alarmConfigVO.getTagsp());
                alarmConfigVO.setTagratepr_name(this.generaterSwapUtil.getDicName(alarmConfigVO.getTagratepr(), "546667323512074629"));
                alarmConfigVO.setTagrate_name(alarmConfigVO.getTagrate());
                alarmConfigVO.setTagratecyc_name(alarmConfigVO.getTagratecyc());
                alarmConfigVO.setAlarmpr_name(this.generaterSwapUtil.getDicName(alarmConfigVO.getAlarmpr(), "546667323512074629"));
                alarmConfigVO.setNoalarmval_name(alarmConfigVO.getNoalarmval());
            }

            List<Map<String, Object>> mapList = JsonUtil.getJsonToListMap(JsonUtil.getObjectToStringDateFormat(listVO, "yyyy-MM-dd HH:mm:ss"));
            mapList.stream().forEach((map) -> {
                Map<String, Object> map2 = new HashMap();
                map.putAll(map2);
            });
            String[] keys = !StringUtil.isEmpty(alarmConfigPagination.getSelectKey()) ? alarmConfigPagination.getSelectKey().split(",") : new String[0];
            UserInfo userInfo = this.userProvider.get();
            DownloadVO vo = this.creatModelExcel(this.configValueUtil.getTemporaryFilePath(), mapList, keys, userInfo);
            return ActionResult.success(vo);
        }
    }

    public DownloadVO creatModelExcel(String path, List<Map<String, Object>> list, String[] keys, UserInfo userInfo) {
        DownloadVO vo = DownloadVO.builder().build();
        List<ExcelExportEntity> entitys = new ArrayList();
        if (keys.length > 0) {
            for(String key : keys) {
                switch (key) {
                    case "tagname":
                        entitys.add(new ExcelExportEntity("点位名称", "tagname_name"));
                        break;
                    case "tagdesc":
                        entitys.add(new ExcelExportEntity("点位描述", "tagdesc_name"));
                        break;
                    case "tagkind":
                        entitys.add(new ExcelExportEntity("点位类型", "tagkind_name"));
                        break;
                    case "tageu":
                        entitys.add(new ExcelExportEntity("工程单位", "tageu_name"));
                        break;
                    case "enabledmark":
                        entitys.add(new ExcelExportEntity("有效标志", "enabledmark_name"));
                        break;
                    case "tagalarmenable":
                        entitys.add(new ExcelExportEntity("是否报警", "tagalarmenable_name"));
                        break;
                    case "tagdeadband":
                        entitys.add(new ExcelExportEntity("报警死区", "tagdeadband_name"));
                        break;
                    case "tagalarmdelay":
                        entitys.add(new ExcelExportEntity("延时时间(ms)", "tagalarmdelay_name"));
                        break;
                    case "updatetime":
                        entitys.add(new ExcelExportEntity("修改时间", "updatetime_name"));
                        break;
                    case "tagllpr":
                        entitys.add(new ExcelExportEntity("低低报优先级", "tagllpr_name"));
                        break;
                    case "tagll":
                        entitys.add(new ExcelExportEntity("低低限值", "tagll_name"));
                        break;
                    case "taglopr":
                        entitys.add(new ExcelExportEntity("低报优先级", "taglopr_name"));
                        break;
                    case "taglo":
                        entitys.add(new ExcelExportEntity("低限值", "taglo_name"));
                        break;
                    case "taghipr":
                        entitys.add(new ExcelExportEntity("高报优先级", "taghipr_name"));
                        break;
                    case "taghi":
                        entitys.add(new ExcelExportEntity("高限值", "taghi_name"));
                        break;
                    case "taghhpr":
                        entitys.add(new ExcelExportEntity("高高报优先级", "taghhpr_name"));
                        break;
                    case "taghh":
                        entitys.add(new ExcelExportEntity("高高限值", "taghh_name"));
                        break;
                    case "tagdevpr":
                        entitys.add(new ExcelExportEntity("偏差优先级", "tagdevpr_name"));
                        break;
                    case "tagdev":
                        entitys.add(new ExcelExportEntity("偏差值", "tagdev_name"));
                        break;
                    case "tagsp":
                        entitys.add(new ExcelExportEntity("偏差设定值", "tagsp_name"));
                        break;
                    case "tagratepr":
                        entitys.add(new ExcelExportEntity("变化率优先级", "tagratepr_name"));
                        break;
                    case "tagrate":
                        entitys.add(new ExcelExportEntity("变化率值", "tagrate_name"));
                        break;
                    case "tagratecyc":
                        entitys.add(new ExcelExportEntity("变化率周期(s)", "tagratecyc_name"));
                        break;
                    case "alarmpr":
                        entitys.add(new ExcelExportEntity("开关量报警优先级", "alarmpr_name"));
                        break;
                    case "noalarmval":
                        entitys.add(new ExcelExportEntity("正常状态值", "noalarmval_name"));
                }
            }

            List<String> keylist = new ArrayList();

            for(String key : keys) {
                keylist.add(key);
            }
        }

        ExportParams exportParams = new ExportParams((String)null, "表单信息");
        exportParams.setType(ExcelType.XSSF);

        try {
            Workbook workbook = new HSSFWorkbook();

            try {
                if (entitys.size() > 0) {
                    if (list.size() == 0) {
                        list.add(new HashMap());
                    }

                    workbook = ExcelExportUtil.exportExcel(exportParams, entitys, list);
                }

                String fileName = "表单信息" + DateUtil.dateNow("yyyyMMdd") + "_" + RandomUtil.uuId() + ".xlsx";
                MultipartFile multipartFile = ExcelUtil.workbookToCommonsMultipartFile(workbook, fileName);
                String temporaryFilePath = this.configValueUtil.getTemporaryFilePath();
                FileInfo fileInfo = FileUploadUtils.uploadFile(multipartFile, temporaryFilePath, fileName);
                vo.setName(fileInfo.getFilename());
                vo.setUrl(UploaderUtil.uploaderFile(fileInfo.getFilename() + "#Temporary") + "&name=" + fileName);
            } finally {
                if (Collections.singletonList(workbook).get(0) != null) {
                    workbook.close();
                }

            }
        } catch (Exception e) {
            log.error("信息导出Excel错误:{}", e.getMessage());
            e.printStackTrace();
        }

        return vo;
    }

    @ApiOperation("上传文件")
    @PostMapping({"/Uploader"})
    public ActionResult<Object> Uploader() {
        List<MultipartFile> list = UpUtil.getFileAll();
        MultipartFile file = (MultipartFile)list.get(0);
        if (!file.getOriginalFilename().endsWith(".xlsx") && !file.getOriginalFilename().endsWith(".xls")) {
            return ActionResult.fail("选择文件不符合导入");
        } else {
            String filePath = XSSEscape.escape(this.configValueUtil.getTemporaryFilePath());
            String fileName = XSSEscape.escape(RandomUtil.uuId() + "." + UpUtil.getFileType(file));
            FileInfo fileInfo = FileUploadUtils.uploadFile(file, filePath, fileName);
            DownloadVO vo = DownloadVO.builder().build();
            vo.setName(fileInfo.getFilename());
            return ActionResult.success(vo);
        }
    }

    @ApiOperation("模板下载")
    @GetMapping({"/TemplateDownload"})
    public ActionResult<DownloadVO> TemplateDownload() {
        DownloadVO vo = DownloadVO.builder().build();
        UserInfo userInfo = this.userProvider.get();
        Map<String, Object> dataMap = new HashMap();
        List<ExcelExportEntity> entitys = new ArrayList();
        entitys.add(new ExcelExportEntity("点位名称", "tagname"));
        entitys.add(new ExcelExportEntity("点位描述", "tagdesc"));
        entitys.add(new ExcelExportEntity("点位类型", "tagkind"));
        entitys.add(new ExcelExportEntity("工程单位", "tageu"));
        entitys.add(new ExcelExportEntity("有效标志", "enabledmark"));
        entitys.add(new ExcelExportEntity("是否报警", "tagalarmenable"));
        entitys.add(new ExcelExportEntity("修改时间", "updatetime"));
        dataMap.put("updatetime", "系统自动生成");
        entitys.add(new ExcelExportEntity("报警死区", "tagdeadband"));
        entitys.add(new ExcelExportEntity("延时时间(ms)", "tagalarmdelay"));
        entitys.add(new ExcelExportEntity("低低报优先级", "tagllpr"));
        entitys.add(new ExcelExportEntity("低低限值", "tagll"));
        entitys.add(new ExcelExportEntity("低报优先级", "taglopr"));
        entitys.add(new ExcelExportEntity("低限值", "taglo"));
        entitys.add(new ExcelExportEntity("高报优先级", "taghipr"));
        entitys.add(new ExcelExportEntity("高限值", "taghi"));
        entitys.add(new ExcelExportEntity("高高报优先级", "taghhpr"));
        entitys.add(new ExcelExportEntity("高高限值", "taghh"));
        entitys.add(new ExcelExportEntity("偏差优先级", "tagdevpr"));
        entitys.add(new ExcelExportEntity("偏差值", "tagdev"));
        entitys.add(new ExcelExportEntity("偏差设定值", "tagsp"));
        entitys.add(new ExcelExportEntity("变化率优先级", "tagratepr"));
        entitys.add(new ExcelExportEntity("变化率值", "tagrate"));
        entitys.add(new ExcelExportEntity("变化率周期(s)", "tagratecyc"));
        entitys.add(new ExcelExportEntity("开关量报警优先级", "alarmpr"));
        entitys.add(new ExcelExportEntity("正常状态值", "noalarmval"));
        List<Map<String, Object>> list = new ArrayList();
        list.add(dataMap);
        ExportParams exportParams = new ExportParams((String)null, "报警配置模板");
        exportParams.setType(ExcelType.XSSF);

        try {
            Workbook workbook = new HSSFWorkbook();

            try {
                if (entitys.size() > 0) {
                    if (list.size() == 0) {
                        list.add(new HashMap());
                    }

                    workbook = ExcelExportUtil.exportExcel(exportParams, entitys, list);
                }

                String fileName = "报警配置模板" + DateUtil.dateNow("yyyyMMddHHmmss") + ".xlsx";
                MultipartFile multipartFile = ExcelUtil.workbookToCommonsMultipartFile(workbook, fileName);
                String temporaryFilePath = this.configValueUtil.getTemporaryFilePath();
                FileInfo fileInfo = FileUploadUtils.uploadFile(multipartFile, temporaryFilePath, fileName);
                vo.setName(fileInfo.getFilename());
                vo.setUrl(UploaderUtil.uploaderFile(fileInfo.getFilename() + "#Temporary") + "&name=" + fileName);
            } finally {
                if (Collections.singletonList(workbook).get(0) != null) {
                    workbook.close();
                }

            }
        } catch (Exception e) {
            log.error("模板信息导出Excel错误:{}", e.getMessage());
            e.printStackTrace();
        }

        return ActionResult.success(vo);
    }

    @ApiOperation("导入预览")
    @GetMapping({"/ImportPreview"})
    public ActionResult<Map<String, Object>> ImportPreview(String fileName) throws Exception {
        Map<String, Object> headAndDataMap = new HashMap(2);
        String filePath = FileUploadUtils.getLocalBasePath() + this.configValueUtil.getTemporaryFilePath();
        FileUploadUtils.downLocal(this.configValueUtil.getTemporaryFilePath(), filePath, fileName);
        File temporary = new File(XSSEscape.escapePath(filePath + fileName));
        int headerRowIndex = 1;
        ImportParams params = new ImportParams();
        params.setTitleRows(0);
        params.setHeadRows(headerRowIndex);
        params.setNeedVerify(true);

        try {
            List<AlarmConfigExcelVO> excelDataList = ExcelImportUtil.importExcel(temporary, AlarmConfigExcelVO.class, params);
            List<ExcelImFieldModel> columns = new ArrayList();
            columns.add(new ExcelImFieldModel("tagname", "点位名称"));
            columns.add(new ExcelImFieldModel("tagdesc", "点位描述"));
            columns.add(new ExcelImFieldModel("tagkind", "点位类型"));
            columns.add(new ExcelImFieldModel("tageu", "工程单位"));
            columns.add(new ExcelImFieldModel("enabledmark", "有效标志"));
            columns.add(new ExcelImFieldModel("tagalarmenable", "是否报警"));
            columns.add(new ExcelImFieldModel("updatetime", "修改时间"));
            columns.add(new ExcelImFieldModel("tagdeadband", "报警死区"));
            columns.add(new ExcelImFieldModel("tagalarmdelay", "延时时间(ms)"));
            columns.add(new ExcelImFieldModel("tagllpr", "低低报优先级"));
            columns.add(new ExcelImFieldModel("tagll", "低低限值"));
            columns.add(new ExcelImFieldModel("taglopr", "低报优先级"));
            columns.add(new ExcelImFieldModel("taglo", "低限值"));
            columns.add(new ExcelImFieldModel("taghipr", "高报优先级"));
            columns.add(new ExcelImFieldModel("taghi", "高限值"));
            columns.add(new ExcelImFieldModel("taghhpr", "高高报优先级"));
            columns.add(new ExcelImFieldModel("taghh", "高高限值"));
            columns.add(new ExcelImFieldModel("tagdevpr", "偏差优先级"));
            columns.add(new ExcelImFieldModel("tagdev", "偏差值"));
            columns.add(new ExcelImFieldModel("tagsp", "偏差设定值"));
            columns.add(new ExcelImFieldModel("tagratepr", "变化率优先级"));
            columns.add(new ExcelImFieldModel("tagrate", "变化率值"));
            columns.add(new ExcelImFieldModel("tagratecyc", "变化率周期(s)"));
            columns.add(new ExcelImFieldModel("alarmpr", "开关量报警优先级"));
            columns.add(new ExcelImFieldModel("noalarmval", "正常状态值"));
            headAndDataMap.put("dataRow", excelDataList);
            headAndDataMap.put("headerRow", columns);
        } catch (Exception e) {
            e.printStackTrace();
            return ActionResult.fail("表头名称不可更改,表头行不能删除");
        }

        return ActionResult.success(headAndDataMap);
    }

    @ApiOperation("导入数据")
    @PostMapping({"/ImportData"})
    public ActionResult<ExcelImportModel> ImportData(@RequestBody VisualImportModel visualImportModel) throws Exception {
        List<AlarmConfigExcelVO> alarmConfigVOList = JsonUtil.getJsonToList(visualImportModel.getList(), AlarmConfigExcelVO.class);
        ExcelImportModel excelImportModel = new ExcelImportModel();
        List<Map<String, Object>> errorList = new ArrayList();

        for(AlarmConfigExcelVO vo : alarmConfigVOList) {
            Map<String, Object> map = JsonUtil.entityToMap(vo);
            Map<String, Object> erroMap = JsonUtil.entityToMap(vo);
            StringJoiner error = new StringJoiner(",");
            Map<String, Object> localcheMap = new HashMap();
            String mainID = RandomUtil.uuId();
            Map<String, Object> var14 = this.generaterSwapUtil.putCache(localcheMap);
            String alarmConfigid = null;
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("tagname").getAnnotation(JnpfField.class), String.valueOf(vo.getTagname()), map, "", var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("tagdesc").getAnnotation(JnpfField.class), String.valueOf(vo.getTagdesc()), map, "", var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("tagkind").getAnnotation(JnpfField.class), String.valueOf(vo.getTagkind()), map, var14, error);
            alarmConfigid = this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("tageu").getAnnotation(JnpfField.class), String.valueOf(vo.getTageu()), map, "", var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("enabledmark").getAnnotation(JnpfField.class), String.valueOf(vo.getEnabledmark()), map, "", var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("tagalarmenable").getAnnotation(JnpfField.class), String.valueOf(vo.getTagalarmenable()), map, "", var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("updatetime").getAnnotation(JnpfField.class), String.valueOf(vo.getUpdatetime()), map, "", var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("tagdeadband").getAnnotation(JnpfField.class), String.valueOf(vo.getTagdeadband()), map, "", var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("tagalarmdelay").getAnnotation(JnpfField.class), String.valueOf(vo.getTagalarmdelay()), map, "", var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("tagllpr").getAnnotation(JnpfField.class), String.valueOf(vo.getTagllpr()), map, var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("tagll").getAnnotation(JnpfField.class), String.valueOf(vo.getTagll()), map, "", var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("taglopr").getAnnotation(JnpfField.class), String.valueOf(vo.getTaglopr()), map, var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("taglo").getAnnotation(JnpfField.class), String.valueOf(vo.getTaglo()), map, "", var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("taghipr").getAnnotation(JnpfField.class), String.valueOf(vo.getTaghipr()), map, var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("taghi").getAnnotation(JnpfField.class), String.valueOf(vo.getTaghi()), map, "", var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("taghhpr").getAnnotation(JnpfField.class), String.valueOf(vo.getTaghhpr()), map, var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("taghh").getAnnotation(JnpfField.class), String.valueOf(vo.getTaghh()), map, "", var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("tagdevpr").getAnnotation(JnpfField.class), String.valueOf(vo.getTagdevpr()), map, var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("tagdev").getAnnotation(JnpfField.class), String.valueOf(vo.getTagdev()), map, "", var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("tagsp").getAnnotation(JnpfField.class), String.valueOf(vo.getTagsp()), map, "", var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("tagratepr").getAnnotation(JnpfField.class), String.valueOf(vo.getTagratepr()), map, var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("tagrate").getAnnotation(JnpfField.class), String.valueOf(vo.getTagrate()), map, "", var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("tagratecyc").getAnnotation(JnpfField.class), String.valueOf(vo.getTagratecyc()), map, "", var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("alarmpr").getAnnotation(JnpfField.class), String.valueOf(vo.getAlarmpr()), map, var14, error);
            this.generaterSwapUtil.excelCheckForm((JnpfField)vo.getClass().getDeclaredField("noalarmval").getAnnotation(JnpfField.class), String.valueOf(vo.getNoalarmval()), map, var14, error);
            if (error.length() > 0) {
                erroMap.put("errorsInfo", error.toString());
                errorList.add(erroMap);
            } else {
                if (alarmConfigid != null) {
                    this.delete(alarmConfigid);
                }

                AlarmConfigEntity alarmConfigEntity = (AlarmConfigEntity)JsonUtil.getJsonToBean(map, AlarmConfigEntity.class);
                alarmConfigEntity.setId(mainID);
                this.alarmConfigService.save(alarmConfigEntity);
            }
        }

        excelImportModel.setSnum(alarmConfigVOList.size() - errorList.size());
        excelImportModel.setFnum(errorList.size());
        excelImportModel.setResultType(errorList.size() > 0 ? 1 : 0);
        excelImportModel.setFailResult(errorList);
        return ActionResult.success(excelImportModel);
    }

    @ApiOperation("导出异常报告")
    @PostMapping({"/ImportExceptionData"})
    public ActionResult<DownloadVO> ImportExceptionData(@RequestBody VisualImportModel visualImportModel) {
        DownloadVO vo = DownloadVO.builder().build();
        List<AlarmConfigExcelErrorVO> alarmConfigVOList = JsonUtil.getJsonToList(visualImportModel.getList(), AlarmConfigExcelErrorVO.class);
        UserInfo userInfo = this.userProvider.get();

        try {
            Workbook workbook = new HSSFWorkbook();

            try {
                ExportParams exportParams = new ExportParams((String)null, "错误报告");
                exportParams.setType(ExcelType.XSSF);
                workbook = ExcelExportUtil.exportExcel(exportParams, AlarmConfigExcelErrorVO.class, alarmConfigVOList);
                String fileName = "错误报告" + DateUtil.dateNow("yyyyMMdd") + "_" + RandomUtil.uuId() + ".xlsx";
                MultipartFile multipartFile = ExcelUtil.workbookToCommonsMultipartFile(workbook, fileName);
                String temporaryFilePath = this.configValueUtil.getTemporaryFilePath();
                FileInfo fileInfo = FileUploadUtils.uploadFile(multipartFile, temporaryFilePath, fileName);
                vo.setName(fileInfo.getFilename());
                vo.setUrl(UploaderUtil.uploaderFile(fileInfo.getFilename() + "#Temporary") + "&name=" + fileName);
            } finally {
                if (Collections.singletonList(workbook).get(0) != null) {
                    workbook.close();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ActionResult.success(vo);
    }

    @DeleteMapping({"/batchRemove/{ids}"})
    @Transactional
    @ApiOperation("批量删除")
    public ActionResult batchRemove(@PathVariable("ids") String ids) {
        String[] idList = ids.split(",");
        int i = 0;

        for(String allId : idList) {
            this.delete(allId);
            ++i;
        }

        return i == 0 ? ActionResult.fail("删除失败") : ActionResult.success("删除成功");
    }

    @ApiOperation("信息")
    @GetMapping({"/{id}"})
    public ActionResult<AlarmConfigInfoVO> info(@PathVariable("id") String id) {
        AlarmConfigEntity entity = this.alarmConfigService.getInfo(id);
        AlarmConfigInfoVO vo = (AlarmConfigInfoVO)JsonUtil.getJsonToBean(entity, AlarmConfigInfoVO.class);
        if (vo.getUpdatetime() != null) {
            vo.setUpdatetime(vo.getUpdatetime());
        }

        return ActionResult.success(vo);
    }

    @ApiOperation("表单信息(详情页)")
    @GetMapping({"/detail/{id}"})
    public ActionResult<AlarmConfigInfoVO> detailInfo(@PathVariable("id") String id) {
        ActionResult info = this.info(id);
        Map<String, Object> dataMap = JsonUtil.entityToMap(info.getData());
        AlarmConfigEntity entity = this.alarmConfigService.getInfo(id);
        AlarmConfigInfoVO vo = (AlarmConfigInfoVO)JsonUtil.getJsonToBean(entity, AlarmConfigInfoVO.class);
        vo.setTagname(vo.getTagname());
        vo.setTagdesc(vo.getTagdesc());
        vo.setTagkind(this.generaterSwapUtil.getDicName(vo.getTagkind(), "436925156615521477"));
        vo.setTageu(vo.getTageu());
        vo.setEnabledmark(this.generaterSwapUtil.switchSelectValue(vo.getEnabledmark(), "启用", "禁用"));
        vo.setTagalarmenable(this.generaterSwapUtil.switchSelectValue(vo.getTagalarmenable(), "是", "否"));
        vo.setTagdeadband(vo.getTagdeadband());
        vo.setTagalarmdelay(vo.getTagalarmdelay());
        vo.setUpdatetime(vo.getUpdatetime());
        vo.setTagllpr(this.generaterSwapUtil.getDicName(vo.getTagllpr(), "546667323512074629"));
        vo.setTagll(vo.getTagll());
        vo.setTaglopr(this.generaterSwapUtil.getDicName(vo.getTaglopr(), "546667323512074629"));
        vo.setTaglo(vo.getTaglo());
        vo.setTaghipr(this.generaterSwapUtil.getDicName(vo.getTaghipr(), "546667323512074629"));
        vo.setTaghi(vo.getTaghi());
        vo.setTaghhpr(this.generaterSwapUtil.getDicName(vo.getTaghhpr(), "546667323512074629"));
        vo.setTaghh(vo.getTaghh());
        vo.setTagdevpr(this.generaterSwapUtil.getDicName(vo.getTagdevpr(), "546667323512074629"));
        vo.setTagdev(vo.getTagdev());
        vo.setTagsp(vo.getTagsp());
        vo.setTagratepr(this.generaterSwapUtil.getDicName(vo.getTagratepr(), "546667323512074629"));
        vo.setTagrate(vo.getTagrate());
        vo.setTagratecyc(vo.getTagratecyc());
        vo.setAlarmpr(this.generaterSwapUtil.getDicName(vo.getAlarmpr(), "546667323512074629"));
        vo.setNoalarmval(vo.getNoalarmval());
        return ActionResult.success(vo);
    }

    @PutMapping({"/{id}"})
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid AlarmConfigForm alarmConfigForm) throws DataException {
        String b = this.alarmConfigService.checkForm(alarmConfigForm, 1);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        } else {
            UserInfo userInfo = this.userProvider.get();
            AlarmConfigEntity entity = this.alarmConfigService.getInfo(id);
            if (entity != null) {
                alarmConfigForm.setUpdatetime(DateUtil.getNow());
                AlarmConfigEntity subentity = (AlarmConfigEntity)JsonUtil.getJsonToBean(alarmConfigForm, AlarmConfigEntity.class);
                boolean b1 = this.alarmConfigService.updateById(subentity);
                return !b1 ? ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据") : ActionResult.success("更新成功");
            } else {
                return ActionResult.fail("更新失败，数据不存在");
            }
        }
    }

    @ApiOperation("删除")
    @DeleteMapping({"/{id}"})
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        AlarmConfigEntity alarmConfigEntity = this.alarmConfigService.getInfo(id);
        if (alarmConfigEntity != null) {
            Map<String, Object> params = new HashMap();
            List<ScadaEntity> itmeList = new ArrayList();
            ScadaEntity entityParam = new ScadaEntity();
            entityParam.setName(alarmConfigEntity.getTagname() + ".ALMENAB");
            entityParam.setVal("0");
            itmeList.add(entityParam);
            params.put("items", itmeList);
            String realdata = this.iviewService.realdata((String)null, JsonUtil.getObjectToString(params));
            ScadaResult scadaResultUpdate = (ScadaResult)JsonUtil.getJsonToBean(realdata, ScadaResult.class);
            if (scadaResultUpdate.getCode() != 0) {
                return ActionResult.fail("SCADA接口下发失败，请检查点位");
            }

            PointUpdateRecordEntity entity = new PointUpdateRecordEntity();
            UserInfo userInfo = this.userProvider.get();
            entity.setSource("4");
            entity.setCreatoruserid(userInfo.getUserId());
            entity.setCreatortime(DateUtil.getNowDate());
            entity.setPointnmae(entityParam.getName());
            entity.setPointdesc(StringUtil.isNotEmpty(alarmConfigEntity.getTagdesc()) ? alarmConfigEntity.getTagdesc() : "");
            entity.setPointvalue("0");
            this.pointUpdateRecordService.save(entity);
            this.alarmConfigService.delete(alarmConfigEntity);
        }

        return ActionResult.success("删除成功");
    }

    @ApiOperation("同步数据")
    @GetMapping({"/syncAll"})
    @Transactional
    public ActionResult<AlarmConfigEntity> syncAll() {
        this.alarmConfigService.syncAll();
        return ActionResult.success("同步已经完成");
    }
}
